package com.suray.rgv.util;

import com.suray.type.ErrorLevel;

import java.util.Objects;

/**
 * 故障信息对象
 */
public class ErrorInfo {
    /**
     * 故障码
     */
    private int errorCode;

    /**
     * 故障信息
     */
    private String errorMessage;

    /**
     * 故障等级
     * 1级故障最严重
     */
    private ErrorLevel errorLevel;

    public ErrorInfo(int errorCode, String errorMessage, ErrorLevel errorLevel) {
        this.errorCode = errorCode;
        this.errorMessage  = errorMessage;
        this.errorLevel = errorLevel;
    }

    public ErrorInfo(ErrorInfo errorInfo) {
        this.errorCode = errorInfo.errorCode;
        this.errorMessage  = errorInfo.errorMessage;
        this.errorLevel = errorInfo.errorLevel;
    }

    public ErrorInfo(int errorCode, String errorMessage, int errorLevel) {
        this.errorCode = errorCode;
        this.errorMessage  = errorMessage;
        this.errorLevel = ErrorLevel.parseLevel(errorLevel);
    }

    public int getErrorCode() {
        return errorCode;
    }

    public String getErrorMessage() {
        return errorMessage;
    }

    public ErrorLevel getErrorLevel() {
        return errorLevel;
    }

    /**
     * 升级故障
     * @param
     * @return
     */
    public boolean updateError(ErrorLevel errorLevel, String message) {
        if (this.errorLevel.getValue() <= errorLevel.getValue()) {
            return false;
        }

        this.errorMessage = message;
        this.errorLevel = errorLevel;
        return true;
    }

    /**
     * 升级故障
     * @return
     */
    public boolean updateError() {
        if (errorLevel == ErrorLevel.ERROR_LEVEL_1) {
            return false;
        }

        errorLevel = ErrorLevel.parseLevel(errorLevel.getValue() - 1);

        return true;
    }

    /**
     * 比较故障等级
     * 当前故障等级高于时返回true
     * @param errorLevel
     * @return
     */
    public boolean compareLevel(ErrorLevel errorLevel) {
        if (this.errorLevel.getValue() < errorLevel.getValue()) {
            return true;
        }

        return false;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        ErrorInfo errorInfo = (ErrorInfo) o;
        return errorCode == errorInfo.errorCode &&
                errorLevel.equals(errorInfo.getErrorLevel());
    }

    @Override
    public int hashCode() {
        return Objects.hash(errorCode, errorLevel);
    }

    @Override
    public String toString() {
        return "ErrorInfo{" +
                "errorCode=" + errorCode +
                ", errorMessage='" + errorMessage + '\'' +
                ", errorLevel=" + errorLevel +
                '}';
    }
}
